{% extends 'app01/base_backend.html' %}
{% block person_manage %}
    <a class="navbar-brand" href="#">资源修改</a>
{% endblock %}
{% block all_li %}
    <li role="presentation">
        <a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">资源修改</a>
    </li>
    <li role="presentation" class="active">
        <a href="{% url 'app01_backend' %}" style="color: #337ab7">返回个人管理</a></li>
{% endblock %}

{% block all_content %}
    {% block caogao %}
        <div class="panel panel-default">
            <div class="panel-heading">资源修改</div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-8 col-md-offset-2">

                        资源名：
                        <input type="text" class="form-control" id="source_name_id" name="sou_name" value="{{ source.name }}" disabled>
                        修改资源描述：
                        <input type="text" class="form-control" id="source_desc_id" name="sou_desc" value="{{ source.descn }}">
                        <br>
                        修改运行效果图：
                        <input type="file" class="form-control" id="source_img_id" name="sou_img">
                        <br>
                        修改代码文件：
                        <input type="file" class="form-control" id="source_code_id" name="sou_code">
                        <br>
                        <input type="text" hidden value="{{ bck_url }}" id="back_url">
                        <button class="btn btn-primary btn-block" id="submit_up_id">提交</button>

                    </div>
                </div>
            </div>
        </div>




    {% endblock %}

{% endblock %}
{% block js %}
    <script>
        if ('{{ request.user.username }}'.slice(0, 3) === '512') {
            $('#p_for_a').html('<a href="/app01/manage_student/" class="list-group-item">学生管理</a>')
            $('#role_for').html('角色：<br><a href="#">老师</a>')
        }


        if ('{{ request.user.username }}'.slice(0, 3) === '312') {

            $('#set_person_id').html('<p>班级：<input type="text" id="stu_class" class="form-control" value="{{ request.user.stu_class }}"></p>')
            $('#role_for').html('角色：<br><a href="#">学生</a>')
        }


        //ajax实现资源修改数据上传
    $('#submit_up_id').click(function () {
        let sou_desc = $('#source_desc_id').val()
        let sou_img = $('#source_img_id')[0].files[0]
        let sou_code = $('#source_code_id')[0].files[0]
        let url = $('#back_url').val()
        let formdata = new FormData()
        formdata.append('sou_desc',sou_desc)
        formdata.append('sou_img',sou_img)
         formdata.append('sou_code',sou_code)
        formdata.append('sou_id','{{source_id}}')
        formdata.append('url',url)
       if(sou_desc===null){
           alert('资源描述不能未空。')
       }else {

              $.ajax({
            url:'',
            type:'post',
            data:formdata,
            contentType:false,
            processData:false,
            success:function (attr) {
                if(attr.code===100){
                    alert(attr.msg)
                    parent.location.href=attr.url
                }
                else {
                    alert(attr.msg)
                }

            }
        })
       }

    })

     //文件上传的ajax请求
        $("#up_btn_id").click(function () {
            let formObj = new FormData()
            formObj.append('file', $('#up_file_id')[0].files[0])
            formObj.append('filename', $('#file_name_id').val())
            formObj.append('desc', $('#file_desc_id').val())
            let radio_chioce = $('input[name="share"]:checked').val();
            formObj.append('share', radio_chioce)

            $.ajax({
                url: "/app01/upload_file/",
                type: 'post',
                data: formObj,
                contentType: false,
                processData: false,
                success: function (array) {
                    if (array.code === 1000) {
                        alert(array.msg0)
                        parent.location.href = array.url
                    } else if (array.code === 2000) {
                        //用户输入不正确的提示
                        $('#p1').next().text(array.msg1)
                        $('#p2').next().text(array.msg2)
                        $('#p3').next().text(array.msg3)
                    } else {
                        alert(array.msg0)
                    }

                }
            })


        });
    </script>
{% endblock %}